<?php

namespace App\Http\Controllers\Api;

use App\Model\ShopOrder;
use App\Model\ShopOrderFs;
use App\Model\ShopOrderFsModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;


class OrderController extends Controller
{
	//订单列表
	public function index(){
		$data = [];
		$datas = [];
		//用户订单
		$list = ShopOrder::where('uid',request()->uid)->orderBy('created_at','desc')->get();
		if($list){
			foreach($list as $k=>$v){
				//订单号
				$data[$k]['ordernum'] = $v['sn'];
				//子订单
				$data[$k]['son'] = ShopOrderFsModel::getSon($v['id']);
				//总价
				$data[$k]['sum_price'] = ShopOrderFsModel::where('oid',$v['id'])->sum('all_price');
				//总数量
				$data[$k]['sum_num'] = ShopOrderFsModel::where('oid',$v['id'])->sum('num');
				if(!$data[$k]['son']){
					unset($data[$k]);
				}
			}
			$data = array_values($data);
		}
		$datas['list'] = $data;
		return ['code'=>1,'msg'=>"返回数据",'data'=>$datas];
	}

	//收货
	public function receipt(){

		$order = ShopOrderFsModel::where('id',request()->id)->where('is_delete',2)->first();
		if(!$order){
			return ['code'=>0,'msg'=>retStrInfo(100)];
		}
		if($order->state != 2){
			return ['code'=>0,'msg'=>retStrInfo(101)];
		}
		$paypass = request()->paypass;
		if(!$paypass){
			return ['code'=>0,'msg'=>'请输入二级密码'];
		}
		$user = UserModel::where('id',request()->uid)->first();
        //支付密码
        if (!Hash::check($paypass, $user['paypass'])) {
            return ['code' => 0, 'msg' => '支付密码输入有误'];
        }
		DB::beginTransaction();
		try{
			ShopOrderFsModel::where('id',request()->id)->update(['state'=>3,'over_time'=>date('Y-m-d H:i:s')]);
			DB::commit();
			return ['code'=>1,'msg'=>'已收货'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msd'=>'操作失败'];
		}
	}

	//全部订单,待发货,待收货,已完成
	public function type_order(){
		$data['list'] = [];
		$list = ShopOrderFsModel::where(function($query){
			if(request()->state){
				$query->where('state',request()->state);
			}
		})->where('is_delete',2)->where('uid',request()->uid)->orderBy('created_at','desc')->get();
		foreach($list as $k=>$v){
			$data['list'][$k]['id'] = $v['id'];
			$data['list'][$k]['sn_son'] = $v['sn_son'];
			$data['list'][$k]['state'] = $v['state'];
			switch($v['state']) {
				case '1':
					$data['list'][$k]['state_name'] = '待发货';
					break;
				case '2':
					$data['list'][$k]['state_name'] = '待收货';
					break;
				case '3':
					$data['list'][$k]['state_name'] = '已完成';
					break;
			}
			if($v['shop_attr']){
				$data['list'][$k]['shop_attr'] = $v['shop_attr'];
			}else{
				$data['list'][$k]['shop_attr'] = '无规格';
			}
			$data['list'][$k]['shop_name'] = $v['shop_name'];

			$data['list'][$k]['single_price'] = $v['single_price'];
			$data['list'][$k]['num'] = $v['num'];
			$data['list'][$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['shop_pic'];
			$data['list'][$k]['all_price'] = $v['all_price'];
		}


		return ['code'=>1,'msg'=>'返回数据','data'=>$data];
	}

//	//订单详情
//	public function detail(){
//		$data = [];
//		$list = ShopOrderFs::where('id',request()->id)->first();
//		if(!$list){
//			return ['code'=>0,'msg'=>retStrInfo(100)];
//		}
//
//		$data['id'] = $list['id'];
//		$data['linkman'] = $list['name'];
//		$data['linktel'] = $list['phone'];
//		$data['address'] = $list['address'];
//		$data['shop_pic'] = "http://".$_SERVER['HTTP_HOST'].$list['shop_pic'];
//		$data['shop_name'] = $list['shop_name'];
//		if($list['shop_attr']){
//			$data['shop_attr'] = $list['shop_attr'];
//		}else{
//			$data['shop_attr'] = '无规格';
//		}
//		$data['single_price'] = $list['single_price'];
//		$data['num'] = $list['num'];
//		$data['sn'] = $list['sn_son'];
//		$data['all_price'] = $list['all_price'];
//
//		$data['created_at'] = $list['created_at'];
//
//		return ['code'=>1,'msg'=>'返回数据','data'=>$data];
//	}


	//订单详情
	public function detail(){
		$list = ShopOrderFs::where('id',request()->id)->select('id','name','phone','address','shop_pic','shop_attr','single_price','num','sn_son','all_price','created_at','shop_name')->first();
		if(!$list){
			return ['code'=>0,'msg'=>retStrInfo(100)];
		}
		$list['linkman'] = $list['name'];
		$list['linktel'] = $list['phone'];
		$list['shop_pic'] = "http://".$_SERVER['HTTP_HOST'].$list['shop_pic'];
		if($list['shop_attr']){
			$list['shop_attr'] = $list['shop_attr'];
		}else{
			$list['shop_attr'] = '无规格';
		}
		$list['sn'] = $list['sn_son'];

		return ['code'=>1,'msg'=>'返回数据','data'=>$list];
	}


	//物流
	public function	logistics(){
		if(!request()->id){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}
		$datas = [];
		//订单号
		$orderid = request()->id;
		//订单数据
		$data = ShopOrderFs::where('id',$orderid)->select('id','express_num','express_com','state','address')->first();
		if($data){
			//物流信息
			$a = array();
			if(!empty($data['express_num'])){
				$a = $this->get($data['express_num']);
			}

			if($a){
				$data['express'] = $a['data'];
			}else{
				$data['express'] = [];
			}


			$arr = explode('省',$data['address']);
			if(count($arr)<2){
				$arr = explode('区',$data['address']);

				if(count($arr) < 2){
					$arr = explode('市',$data['address']);
					if(!count($arr) < 2){
						$arr_cy = $data['address'];
					}else{
						$arr_cy = $arr[0].'市';
					}
					
				}else{
					$arr_cy = $arr[0].'区';
				}
				
			}else{
				$arr_cy = $arr[0].'省';
			}
			
			$data['city'] = $arr_cy;
		}else{
			$data = $datas;
		}
		return ['code'=>1,'msg'=>'返回数据','data'=>$data];
	}

	// 查询快递信息
	public function get($num){
		$url = "http://www.kuaidi100.com/autonumber/auto?num={$num}&key=jyFUDdqL8938";
		$str = file_get_contents($url);
		$arr = json_decode($str,true);
		foreach ($arr as $key => $value) {
			$res = $this->express($value['comCode'], $num);
			if($res['message'] == "ok"){
				return $res;
			}
		}
	}

	/**
	物流  快递100
	name   物流公司名称 （shunfeng 顺丰） 可在官网查看物流公司的简称
	number   单号
	 */
	public function express($name,$number){
		//参数设置
		$post_data = array();
		$post_data["customer"] = '8AA707C0A4AFB0A29CC76525E2679114';
		$key= 'jyFUDdqL8938' ;

		$data['com']=$name;  //查询的快递公司的编码， 一律用小写字母
		$data['num']=$number;  //查询的快递单号， 单号的最大长度是32个字符 358263398950
		$post_data["param"] =json_encode($data);

		$url='http://poll.kuaidi100.com/poll/query.do';
		$post_data["sign"] = md5($post_data["param"].$key.$post_data["customer"]);
		$post_data["sign"] = strtoupper($post_data["sign"]);
		$o="";
		foreach ($post_data as $k=>$v)
		{
			$o.= "$k=".urlencode($v)."&";       //默认UTF-8编码格式
		}
		$post_data = substr($o,0,-1);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
		curl_setopt($ch, CURLOPT_TIMEOUT,3);
		$result = curl_exec($ch);
		curl_close($ch);
		$data = json_decode($result,true);
		return $data;
	}

	//删除订单
	public function del(){
		if(!request()->id){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}
		DB::beginTransaction();
		try{
			//订单信息
			$order = ShopOrderFsModel::where('id',request()->id)->first();
			if(!$order){
				return ['code'=>0,'msg'=>retStrInfo(76)];
			}
			ShopOrderFsModel::where('id',request()->id)->update(['is_delete'=>1]);
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(72)];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>retStrInfo(73)];
		}
	}

}